package trading.ctm.yunjing.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;

import app.AppApplication;
import cc.vv.baselibrary.global.JNIBroadCastKey;
import cc.vv.baselibrary.util.LKLogUtil;
import cc.vv.baselibrary.util.LKToastUtil;
import trading.ctm.yunjing.R;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "WXPayEntryActivity";
    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        api = AppApplication.Companion.getMsgApi();
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
        LKLogUtil.e(req.transaction);
    }

    /**
     * 0，成功，展示成功页面
     * -1，错误，签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等。
     * -2，用户取消，无需处理。发生场景：用户不支付了，点击取消，返回APP。
     *
     * @param resp
     */
    @Override
    public void onResp(BaseResp resp) {
        LKLogUtil.e("onPayFinish, errCode = " + resp.errCode);
        if (resp.errCode == 0) {
            LKLogUtil.e("支付成功");
//            LKToastUtil.showToastShort("支付成功");
            sendBroadcast(new Intent(JNIBroadCastKey.WEIXIN_RECHARGE_SUCCESS));
            sendBroadcast(new Intent(JNIBroadCastKey.WEIXIN_SHOP_ORDER_PAY_SUCCESS));
            sendBroadcast(new Intent(JNIBroadCastKey.WEIXIN_TRANSFER).putExtra("code", resp.errCode));
        } else if (resp.errCode == -1) {
            LKLogUtil.e("参数错误");
            LKToastUtil.showToastShort("参数错误");
        } else {
            LKLogUtil.e("用户取消 ");
//            LKToastUtil.showToastShort("用户取消");
            sendBroadcast(new Intent(JNIBroadCastKey.WEIXIN_RECHARGE_CANCEL));
            sendBroadcast(new Intent(JNIBroadCastKey.WEIXIN_SHOP_ORDER_PAY_CANCEL));
            sendBroadcast(new Intent(JNIBroadCastKey.WEIXIN_TRANSFER).putExtra("code", resp.errCode));
        }
        finish();
    }

}